<?php 	namespace Common\Model;
use Hdphp\Model\Model;
class Goods extends Model
{
	protected $table = "goods";
	protected $validate = array(
	array("goods_name","required","商品名称不能为空",3,3),
	array("goods_tid","required","所属分类不能为空",3,3),
	array("goods_bid","required","所属品牌不能为空",3,3),
	array("goods_number","required","商品货号不能为空",3,3),
	array("goods_unit","required","商品单位不能为空",3,3),
	array("goods_marketprice","required","市场价不能为空",3,3),
	array("goods_shopprice","required","商城价不能为空",3,3),
	array("goods_pic","required","列表图不能为空",3,3),
	);
	protected $auto = array(
		array('goods_time','time','function',3,1),
		array('goods_aid','userUid','method',3,1),
		
	
	
	);
	public function userUid()
	{
		return $_SESSION['uid'];
		
	}
	
	
	public function store()
	{
		$detail = new \Common\Model\Detail;
		$GoodsAttr = new \Common\Model\GoodsAttr;
//		p($detail);die;
		if(!$this->create()) return FALSE;
		
		
		$aid  = $this->add();
//		p($aid);die;
		if(isset($_POST['photo']))
		{
			$small  = "";
			$medium = "";
			$big = "";
			$path = "Upload/thumb/" . date("ymd") . "/";
			if(is_dir($path) && !isset($path))
			{
				mkdir($path,0777,TRUE);
				}
				foreach ($_POST['photo'] as $k => $v) {
					$small .=Image::thumb($v,$path."/small/".basename($v),200,200,5).",";
					$medium .=Image::thumb($v,$path."/medium/".basename($v),400,400,5).",";
					$big .=Image::thumb($v,$path."/big/".basename($v),800,800,5).",";
					
				}
				$DetailData = array(
					"detail_small" =>$small,
					"detail_medium" =>$medium,
					"detail_big" =>$big,
					"detail_intro" =>$_POST['goods_intro'],
					"detail_service" =>$_POST['goods_service'],
					"detail_goods_id" =>$aid,
					
				
				);
			$detail->add($DetailData);
			
 		}
		foreach ($_POST['attr'] as $k => $v) {
			$data = array(
				"attr_goods_id"=>$aid,
				"attr_tyattr_id"=>$k,
				"goods_attr_value"=>$v,
			);
			$GoodsAttr->add($data);
		}
		foreach ($_POST['spec'] as $k => $v) {
			foreach ($v['color'] as $k1 => $v1) {
				$data = array(
				"attr_goods_id"=>$aid,
				"attr_tyattr_id"=>$k,
				"goods_attr_value"=>$v1,
				"goods_attr_added"=>$v['added'][$k1],
			);
			$GoodsAttr->add($data);
			}
			
		}
		
		return TRUE;
	}
}
